#include "GY_TransformationClass.h"
#include <QDebug>
//#include<QScriptEngine>

GY_TransformationClass::GY_TransformationClass(QObject *parent) : QObject(parent)
{
    m_mathFunction.insert("Tan","math.tan");
    m_mathFunction.insert("Sin","math.sin");
    m_mathFunction.insert("Cos","math.cos");
    m_mathFunction.insert("ToInt","math.floor");
    m_mathFunction.insert("Sqrt","math.sqrt");
    m_mathFunction.insert("Abs","math.abs");
    m_mathFunction.insert("Ceil","math.ceil");
    m_mathFunction.insert("Bool::TRUE","1");
    m_mathFunction.insert("Bool::FALSE","0");
    m_mathFunction.insert("ToFloat","ToFloat");
    m_mathFunction.insert("IfElse","IfElse");
//    m_mathFunction.insert("!","NOT");
    m_mathFunction.insert("not","NOT");
}

QVector<QString> GY_TransformationClass::formulaTransformation(QString &source, QVector<QString> &tmpValueVec)
{
    QString test = source;
    QString testchange = source;
    QRegExp regNum("^[0-9]");
    QRegExp regAZ("^[a-zA-Z]");
    QVector<QString> tmpVecName;//表达式中原变量名
    for (int i = 0;test.size() > 0 && i < test.size();)
    {
        //第一个字符是字母或者点
        if('.' == test[i] || regAZ.exactMatch(test.at(i)) || '_' == test[i])
        {
            QVector<QString> tmpVec;
            findPointStruct(test,tmpVec);
            if((test.size() != 0) && regNum.exactMatch(test.at(0))) {
                i = 0;
                continue;
            }
            QString variable;
            QString var;

            for(int j = 0;j < tmpVec.size();j++)
            {
                variable += tmpVec.at(j);
                if(j == 0) {
                    var += tmpVec.at(j);
                    var = var.remove(".");
                }else {
                    var += tmpVec.at(j);
                }
            }
            tmpVecName.push_back(variable);
            //testchange
            QString valName = var;//两个点为point类型例如.position.x
//            valName.remove(".");
            testchange = testchange.replace(testchange.indexOf(variable),variable.size(),valName);
            QStringList list = valName.split(".");
            tmpValueVec.push_back(list.at(0));
            i = 0;
        }
        else
        {
            test = test.right(test.size() - i - 1);
        }
    }
    source = testchange;
    QMap<QString,QString>::Iterator iter = m_mathFunction.begin();
    while (iter != m_mathFunction.end()) {
        for(int i=0;i<tmpVecName.size();i++) {
            if(tmpVecName.at(i) == iter.key()) {
                tmpVecName.remove(i);
                tmpValueVec.remove(i);
                source.replace(iter.key(),iter.value());
            }
        }
        ++iter;
    }
    return tmpVecName;
}

void GY_TransformationClass::findPointStruct(QString &strdata, QVector<QString> &tmpVec)
{
    int i = 0;
    while(!strdata.isEmpty())
    {
        QRegExp regNum("^[0-9]");
        QRegExp regAZ("^[a-zA-Z]");
        QString str = strdata.at(i);
        //str为数字或者_或者为(字母并且tmpvec不为空)
        if(regNum.exactMatch(str) || "_" == str || (regAZ.exactMatch(str)&&!tmpVec.isEmpty()))
        {
            i++;
        }
        //str为点或者字母
        else if("." == str || regAZ.exactMatch(str))
        {
            if(strdata.size() == 1) {
                strdata = strdata.remove(0, 1);
                tmpVec.push_back(str);
                return;
            }
            //添加上一层
            if(regNum.exactMatch(strdata.at(++i)))
            {
                strdata = strdata.remove(0, 1);
                //.后面不能跟数字
                return;
            }
            while (i < strdata.size() &&(regNum.exactMatch(strdata.at(i)) || regAZ.exactMatch(strdata.at(i)) || '_' == strdata.at(i)
                                         || '[' == strdata.at(i) || ']' == strdata.at(i) || '*' == strdata.at(i) || ':' == strdata.at(i)))
            {
                ++i;
            }
            int index = i;

            QString ret = strdata.left(index);
            tmpVec.push_back(ret);
            strdata = strdata.right(strdata.size() - index);
            i = 0;
            //开始下一层
            findPointStruct(strdata,tmpVec);
        }
        else
        {
            break;
        }
    }
}


